// pages/account/personalinfo.js
var app = getApp();
var dateTimePicker = require('../../utils/dateTimePicker.js');
var util = require('../../utils/util.js');
import WxValidate from '../../utils/validate';
var inputContent = {};//输入内容
Page({
  data: {
    sexIndex: 3,
    sex: ["男", "女"],
    phone:null,
    name:null
  },
  onLoad: function (options) {

    // 页面初始化 options为页面跳转所带来的参数
    //验证表单
    this.WxValidate = new WxValidate({
      Name: {
        required: true,
        minlength: 2
      },
      TelNum: {
        required: true,
        tel: true
      },
      Gender: {
        required: true
      },
      dataResource:{
        contains:true
      }

    }, {
        Name:
        {
          required: "请输报备客户入姓名",
          minlength: "请输入正确姓名"
        },
        TelNum: {
          required: "请输入报备客户电话",
          tel: "请输入正确电话"
        },
        Gender: {
          required: "请输入报备客户性别",
        },
        dataResource: {
          contains: "抱歉，此客户已被其它经纪人报备"
        }
      
      })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.setData({
      isLogin: util.isLogin(),
      userData: wx.getStorageSync("userData") || ""//用户信息
    })


  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  //获取手机号
  bindPhone: function (e) {
    var pho = e.detail.value;
   
    this.setData({
      date: e.detail.value
    })
    
  },
  //获取用户输入
  bindName: function (e) {
    inputContent[e.currentTarget.id] = e.detail.value;
    console.log(inputContent);
    console.log(e.currentTarget.id);
  },
  bindSexPickerChange:function(e){
    console.log(e.detail.value);
   
    this.setData({
      sexIndex: e.detail.value
    })
    inputContent[e.currentTarget.id] = this.data.sex[this.data.sexIndex];
  },
  /**
   * 个人信息提交数据
   */
  personalInfoSubmit: function (e) {
    //调用验证表单方法
    console.log(e);
    const params = e.detail.value;
    console.log(params);
    inputContent = util.mergeJsonObject(inputContent, params);
    console.log(inputContent);
    e.detail.value = inputContent;
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList
      console.log(error);
      wx.showModal({
        title: '友情提示',
        content: error[0].msg,
        showCancel: false,
        confirmColor: "#f26604",
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定');
          }
        }
      })
      console.log(error)
      return false
    }

    console.log(inputContent);
    util.https("insertRport.shtml", "GET", { "cname": inputContent.Name, "eid": app.globalData.myInfo.eid, "cphone": inputContent.TelNum, "cgender": inputContent.Gender},
    
    function(data){

      console.log("返回的数据"+data.data);
     if(data.data==1){
       wx.showToast({
         title: '登录成功',
         icon: 'success',
         duration: 1000
       })
       wx.switchTab({

         url: '/pages/index/index'
       })
     }else{
      
       wx.showModal({
         title: '友情提示',
         content: "抱歉,该客户已被其它经纪人报备！",
         showCancel: false,
         confirmColor: "#f26604",
         success: function (res) {
           if (res.confirm) {
             console.log('用户点击确定');
           }
         }
       })
       return false;

     }

    }
   
    
    );
  }
})